package com.GlobalPaint.app.ui.Mine.Laborer;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.GlobalPaint.app.Adapter.MYLinshiAdapter;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.LinshiWirkBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.GlobalPaint.app.utils.FirstEvent;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


public class MYLinshiFragment extends Fragment {
    private MYLinshiAdapter adapter;
    private int page = 1;
    private XRecyclerView linshi_xrecyler;
    private LinearLayoutManager linearLayoutManager;
    private LinshiWirkBean bean;
    private List<LinshiWirkBean.DataBean> list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_mylinshi, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        linshi_xrecyler = (XRecyclerView) view.findViewById(R.id.linshi_xrecyler);
        linearLayoutManager = new LinearLayoutManager(getContext());
        linshi_xrecyler.setLayoutManager(linearLayoutManager);
        linshi_xrecyler.setLoadingMoreEnabled(false);
        getList();
        initLoad();
    }

    private void initLoad() {
        linshi_xrecyler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                String url = Constants.JASON_SERVICE_URL + "qlwjobtemphireinfo/list?page=" + 1+
                        "&userid="+ DataManager.userEntity.getUserId();
                DataManager.getInst().getHttpRequestJsonClass(url,DataManager.userEntity.getCookie(), LinshiWirkBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        page = 1;
                        LinshiWirkBean Jobbean = (LinshiWirkBean) o;
                        if (Jobbean.getStatus() == 1) {
                            if (Jobbean.getData() != null && Jobbean.getData().size() > 0) {
                                list.clear();
                                list.addAll(Jobbean.getData());
                                adapter.notifyDataSetChanged();
                                linshi_xrecyler.loadMoreComplete();
                            }
                        }
                        linshi_xrecyler.refreshComplete();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        linshi_xrecyler.refreshComplete();
                    }
                });
            }

            @Override
            public void onLoadMore() {
                page++;
                String url = Constants.JASON_SERVICE_URL + "qlwjobtemphireinfo/list?page="+ page+"&userid="+
                        DataManager.userEntity.getUserId();
                DataManager.getInst().getHttpRequestJsonClass(url,DataManager.userEntity.getCookie(), LinshiWirkBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        LinshiWirkBean jobBean = (LinshiWirkBean) o;
                        if (jobBean.getStatus() == 1) {
                            if (jobBean.getData() != null && jobBean.getData().size() > 0) {
                                list.addAll(jobBean.getData());
                            }
                        } else {
                            page = page - 1;
                            Toast.makeText(getContext(), "暂无更多数据", Toast.LENGTH_LONG).show();
                        }
                        linshi_xrecyler.loadMoreComplete();
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        page = page - 1;
                        Toast.makeText(getContext(), "暂无更多数据", Toast.LENGTH_LONG).show();
                        linshi_xrecyler.loadMoreComplete();
                    }
                });
            }
        });
    }
    public  void deleteUser(final int id,final int position) {
        String url= Constants.JASON_SERVICE_URL+"qlwjobtemphireinfo/delete?id="+id;
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), LinshiWirkBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                bean = (LinshiWirkBean) o;
                if(bean.getStatus()==1){
                    EventBus.getDefault().post(new FirstEvent("linshi"));
                    list.remove(position);
                    adapter.notifyDataSetChanged();
                }
                else {
                    Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {


            }
        });
    }
    private void getList() {

        String url = Constants.JASON_SERVICE_URL + "qlwjobtemphireinfo/list?page=1&userid="+DataManager.userEntity.getUserId();
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), LinshiWirkBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {

                bean = (LinshiWirkBean) o;
                if (bean.getStatus() == 1) {
                    if (bean.getData() != null) {
                        list = bean.getData();
                        adapter = new MYLinshiAdapter(list, MYLinshiFragment.this);
                        linshi_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }else
                    {
                        list=new ArrayList<LinshiWirkBean.DataBean>();
                        adapter = new MYLinshiAdapter(list, MYLinshiFragment.this);
                        linshi_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    Toast.makeText(getContext(), bean.getMsg(), Toast.LENGTH_LONG).show();
                }
                linshi_xrecyler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {

                if (list != null && list.size() > 0) {
                    list.clear();
                    Toast.makeText(getContext(), "获取失败", Toast.LENGTH_LONG).show();
                    adapter.notifyDataSetChanged();
                    linshi_xrecyler.refreshComplete();

                }
            }
        });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}
